<?php

namespace Common\Model;

use Think\Model;

/**
 * 模块开启/完成
 * v1.0.5版本 qy_open_lock表只能查询 章节、课程、专业是否已经开启 和 已完成 状态。 项目中的所还需要其他的逻辑条件去结合做判断。
 * v1.0.6版本 qy_open_lock表只记录【课程完成：type=2 && status=2】【专业打开/完成：type=3 && status=1/2】,其他废弃。
 */
class OpenLockModel extends Model {

    /**
     * 开启模块
     * @param array $arrData
     * @param int user_id 用户ID
     * @param int type 模块 1-章节 2-课程 3-专业
     * @param int relat_id 模块对应ID(章节ID、课程ID、专业ID)
     * @param int company_id 所属企业ID
     * @param int status 状态 1-已完成
     */
    public function openModule($arrData) {
        $whereData = $arrData['where'];
        $changeData = $arrData['change'];

        $openLocakInfo = $this->where($whereData)->find();
        if (empty($openLocakInfo)) {
            $data = array_merge($whereData, $changeData);
            $this->add($data);
        }
    }

    /**
     * 完成模块
     * @param array $arrData
     * @param int user_id 用户ID
     * @param int type 模块 1-章节(废弃) 2-课程 3-专业
     * @param int relat_id 模块对应ID(章节ID、课程ID、专业ID)
     * @param int company_id 所属企业ID
     * @param int status 状态 1-已开启 2-已完成
     * @return boolean
     */
    public function completeModule($arrData) {
        $whereData = $arrData['where'];
        $changeData = $arrData['change'];
        $openLocakInfo = $this->where($whereData)->find();
        if (!empty($openLocakInfo)) {
            $res = $this->where($whereData)->save($changeData);
        } else {
            $data = array_merge($whereData, $changeData);
            $res = $this->add($data);
        }
        if ($res) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 开启或完成专业
     * @param int $iUserId 用户ID
     * @param int $iSpecialtyId 专业ID
     */
    public function openOrCompleteSpecialty($iUserId, $iSpecialtyId, $iStatus = 1) {
        if (!in_array($iStatus, array(1, 2))) {
            return FALSE;
        }
        $specialtyData = array(
            'where' => array(
                'user_id' => $iUserId,
                'type' => 3,
                'relat_id' => $iSpecialtyId
            ),
            'change' => array(
                'status' => $iStatus == 1 ? 1 : 2
            )
        );
        $this->completeModule($specialtyData);
    }

    /**
     * 完成课程
     * @param int $iUserId 用户ID
     * @param int $iCurriculumId 课程ID
     */
    public function completeCurriculum($iUserId, $iCurriculumId) {
        $completeCurriculumData = array(
            'where' => array(
                'user_id' => $iUserId,
                'type' => 2,
                'relat_id' => $iCurriculumId
            ),
            'change' => array(
                'status' => 2
            )
        );
        $this->completeModule($completeCurriculumData);
    }

    /*     * ***********************************************************以下几个方法暂时废弃***************************************************************************** */

    /**
     * 开启新专业（测评完成开启专业 和 学习完成开启下一专业） 
     * （开启专业下第一课程第一章节）
     * @param int specialty_id 要开启的专业ID
     */
    public function openFirstCurriculumAndChapter($iUserId, $iSpecialtyId) {
        if (empty($iUserId) || empty($iSpecialtyId)) {
            return FALSE;
        }
        $specialtyData = array(
            'where' => array(
                'user_id' => $iUserId,
                'type' => 3,
                'relat_id' => $iSpecialtyId
            ),
            'change' => array(
                'status' => 1
            )
        );
        $this->openModule($specialtyData);

        $currInfo = M('Curriculum')->field('curriculum_id')->where(array('specialty_id' => $iSpecialtyId, 'sort' => 1, 'status' => 1))->find();
        if (empty($currInfo)) {
            return FALSE;
        }

        $currData = array(
            'where' => array(
                'user_id' => $iUserId,
                'type' => 2,
                'relat_id' => $currInfo['curriculum_id']
            ),
            'change' => array(
                'status' => 1
            )
        );
        $this->openModule($currData);

        $chapterInfo = M('Chapter')->field('chapter_id')->where(array('curriculum_id' => $currInfo['curriculum_id'], 'sort' => 1, 'status' => 1))->find();
        if (empty($chapterInfo)) {
            return FALSE;
        }
        $chapterData = array(
            'where' => array(
                'user_id' => $iUserId,
                'type' => 1,
                'relat_id' => $chapterInfo['chapter_id']
            ),
            'change' => array(
                'status' => 1
            )
        );
        $this->openModule($chapterData);
    }

    /**
     * 开启专业下全部课程章节
     * (评测通过初级，开启中级和全部初级时调用此方法)
     */
    public function openAllCurrAndChapter($iUserId, $iSpecialtyId) {
        if (empty($iUserId) || empty($iSpecialtyId)) {
            return FALSE;
        }

        $specialtyData = array(
            'where' => array(
                'user_id' => $iUserId,
                'type' => 3,
                'relat_id' => $iSpecialtyId
            ),
            'change' => array(
                'status' => 1
            )
        );
        $this->openModule($specialtyData);

        $currList = M('Curriculum')->field('curriculum_id')->where(array('specialty_id' => $iSpecialtyId, 'status' => 1))->select();
        if (empty($currList)) {
            return FALSE;
        }
        foreach ($currList as $val) {
            $currIds[] = $val['curriculum_id'];
            $arrCurrData[] = array(
                'user_id' => $iUserId,
                'type' => 2,
                'relat_id' => $val['curriculum_id'],
                'status' => 1
            );
        }
        $this->addAll($arrCurrData);

        if (empty($currIds)) {
            return FALSE;
        }
        $chapterList = M('Chapter')->field('chapter_id')->where(array('curriculum_id' => array('in', $currIds), 'status' => 1))->select();
        if (empty($chapterList)) {
            return FALSE;
        }
        foreach ($chapterList as $val) {
            $arrChapterData[] = array(
                'user_id' => $iUserId,
                'type' => 1,
                'relat_id' => $val['chapter_id'],
                'status' => 1
            );
        }
        $this->addAll($arrChapterData);
    }

    /**
     * 已开启的模块：用户在该企业中已开启的章节、课程、专业
     * @param int $iUserId 用户ID
     * @param int $iCompanyId 企业ID
     * @param int $iType 所属模块
     * @return array 返回开启的所有模块
     */
    public function alreadyOpenLock($iUserId, $iType) {
        $arrWhere['user_id'] = $iUserId;
        $arrWhere['type'] = $iType;
        $lockList = $this->where($arrWhere)->select();
        return $lockList;
    }

}
